

export function formatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};
function padLeftZero(str) {
    return ('00' + str).substr(str.length);
}
/**
 * 根据等级显示等级图片
 * @param {*} lvNum 
 */
export function getMemberLV(lvNum) {
    let url = '@/assets/img/';
    if (lvNum == 1) {
        return url + 'dj1.png'
    }
    else if (lvNum == 2) {
        return url + 'dj2.png'
    }
    else if (lvNum == 3) {
        return url + 'dj3.png'
    }
    else if (lvNum == 4) {
        return url + 'dj4.png'
    }
    else if (lvNum == 5) {
        return url + 'dj5.png'
    }
    else {
        return url + 'dj0.png'
    }
}

/**
 * 判断是否微信登陆 是不是微信浏览器
 */
export function isWeiXin() {
    let ua = window.navigator.userAgent.toLowerCase();
    console.log('test: ' + ua);
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        console.log('weixin is true')
        return true;
    } else {
        // this.$vux.alert.show({ title:'提示', content:'请使用微信客户端登录'} )
        console.log('weixin is false')
        return false;
    }
}

/**
 *  获取微信静默登录url, stateurl授权登录成功后跳转URL
 * @param {*} stateurl 
 */
export function getWXLoginUrl(stateurl) {
    return "http://pay.gxmisi.com/v1/api/wxLogin?stateurl=" + stateurl;
}




/**
 * 解析 URL 参数
 * http://localhost:8080/?id=123&key=vaule#/restaurant/seller
 * "?id=123&key=vaule"
 * {id: "123", key: "vaule"}
 */
export function urlParse(url = window.location.search) {
    // window.location.href 当前文件的绝对地址
    // window.location.search 当前文件的哈希地址
 
    let obj = {};
    let reg = /[?&][^?&]+=[^?&]+/g;
    let arr = url.match(reg); // ["?id=123", "&key=vaule"]

    if (arr) {
        arr.forEach(item => {
            // 删除 ? 和 &，以 = 为标志分割数组
            let tempArr = item.substring(1).split("="); // ["id", "123"]  ["key", "vaule"]
            // 使用 decodeURIComponent() 对编码后的 URI 进行解码
            let key = decodeURIComponent(tempArr[0]);
            let value = decodeURIComponent(tempArr[1].replace("/", ""));
            obj[key] = value;
        });
    }
    return obj;
}

/**
 * 拼接成带桌号的链接字符串 
 */
export function getTCodeUrl() {
    let tcode = ''; //餐桌号
    let urlp = urlParse();
    if (urlp.hasOwnProperty("code") != false) {
        tcode = "?code=" + urlp.code;
        localStorage.getItem("tcodeurl"); //保存至localStorage
    }
    return tcode;
}


/*用export把方法暴露出来*/
/*设置cookie
*c_name cookie名称，value 值，expire 失效时间
*/
export function setCookie(c_name, value, expire) {
    var date = new Date()
    date.setSeconds(date.getSeconds() + expire)
    document.cookie = c_name + "=" + escape(value) + "; expires=" + date.toGMTString()
    console.log(document.cookie)
}

/*获取cookie*/
export function getCookie(c_name) {
    if (document.cookie.length > 0) {
        let c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            let c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}
/*删除cookie*/
export function delCookie(c_name) {
    setCookie(c_name, "", -1)
}
